//
//  NMPODeviceResultCell.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPODeviceResultCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(nmpo_iconView1)
        contentView.addSubview(nmpo_iconView3)
        contentView.addSubview(nmpo_iconView2)
        contentView.addSubview(nmpo_collectionView)
        nmpo_iconView1.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 172, height: 212))
            make.top.left.equalToSuperview()
        }
        nmpo_iconView2.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(20)
            make.size.equalTo(112)
        }
        nmpo_iconView3.snp.makeConstraints { make in
            make.right.centerY.equalToSuperview()
            make.height.equalTo(172)
            make.left.equalTo(nmpo_iconView2.snp.centerX)
        }
        nmpo_collectionView.snp.makeConstraints { make in
            make.top.bottom.right.equalTo(nmpo_iconView3)
            make.left.equalTo(nmpo_iconView2.snp.right)
        }
        nmpo_collectionView.register(NMPODeviceResultItemCell.classForCoder(), forCellWithReuseIdentifier: NMPODeviceResultItemCell.nmpo_viewToString)
        nmpo_collectionView.dataSource = self
        nmpo_collectionView.contentInset = UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 12)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - nmpo lazy
    lazy var nmpo_iconView1: UIImageView = {
        let nmpo_iconView = UIImageView(image: UIImage(named: "nmpo_result_scanDeviceBg"))
        return nmpo_iconView
    }()
    lazy var nmpo_iconView2: UIImageView = {
        let nmpo_iconView = UIImageView(image: UIImage(named: "nmpo_result_scanDevice"))
        return nmpo_iconView
    }()
    lazy var nmpo_iconView3: UIImageView = {
        let nmpo_iconView = UIImageView(image: UIImage(named: "nmpo_result_testInfo"))
        return nmpo_iconView
    }()
    @objc  func nmpo_layout() -> UICollectionViewFlowLayout {
        let nmpo_layout = UICollectionViewFlowLayout()
        nmpo_layout.itemSize =  CGSize(width: nmpo_SCREEN_WIDTH-40-112-30, height: 16)
        nmpo_layout.minimumLineSpacing = 8
        return nmpo_layout
    }
    
    lazy var nmpo_collectionView: UICollectionView = {
        let nmpo_collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: nmpo_layout())
        nmpo_collectionView.backgroundColor = .clear
        nmpo_collectionView.showsVerticalScrollIndicator = false
        nmpo_collectionView.showsHorizontalScrollIndicator = false
        nmpo_collectionView.contentInsetAdjustmentBehavior = .never
        return nmpo_collectionView
    }()
    var nmpo_deviceModels:[NMPODeviceModel]? {
        didSet {
            nmpo_collectionView.reloadData()
        }
    }
}
// MARK: - lazy UICollectionViewDataSource
extension NMPODeviceResultCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return nmpo_deviceModels?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let nmpo_deviceModel = nmpo_deviceModels?[indexPath.row]
        let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPODeviceResultItemCell.nmpo_viewToString, for: indexPath) as! NMPODeviceResultItemCell
        nmpo_cell.nmpo_titleLabel.text = nmpo_deviceModel?.nmpo_title
        nmpo_cell.nmpo_decLabel.text = nmpo_deviceModel?.nmpo_dec
        return nmpo_cell
    }
}
//MARK: - nmpo device cell
class NMPODeviceResultItemCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(nmpo_titleLabel)
        contentView.addSubview(nmpo_decLabel)
        nmpo_titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview()
            make.width.equalTo(90)
        }
        nmpo_decLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview()
            make.left.equalTo(nmpo_titleLabel.snp.right).offset(5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - nmpo lazy
    lazy var nmpo_titleLabel: UILabel = {
        let nmpo_titleLabel = UILabel()
        nmpo_titleLabel.font = UIFont.systemFont(ofSize: 12)
        nmpo_titleLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0x878787)
        nmpo_titleLabel.text = "Device name："
        return nmpo_titleLabel
    }()
    lazy var nmpo_decLabel: UILabel = {
        let nmpo_decLabel = UILabel()
        nmpo_decLabel.font = UIFont.systemFont(ofSize: 12)
        nmpo_decLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0xFFFFFF)
        nmpo_decLabel.text = "-"
        nmpo_decLabel.textAlignment = .right
        return nmpo_decLabel
    }()
}
